package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.third.JuheCityMapper;
import com.zjdiepu.www.ibiz.third.IJuheCityService;
import com.zjdiepu.www.model.third.dto.JuheCity;
import com.zjdiepu.www.model.third.ext.JuheCityExt;

/**
 * 城市Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月18日 18时03分<br/>
 * @since 1.0<br/>
 */
@Service
public class JuheCityServiceImpl extends BaseService implements IJuheCityService {
	public static final Logger logger = LoggerFactory.getLogger(JuheCityServiceImpl.class);

	@Autowired
	private JuheCityMapper juheCityMapper;
	
	@Override
    public String getNameById(String cityId) {
        if(StringUtils.isNotBlank(cityId)) {
            JuheCity juheCity = new JuheCity();
            juheCity.setCityId(cityId);
            juheCity.setDpStatus(DpStatus.NORMAL.getCode());
            juheCity = juheCityMapper.selectRefined(juheCity);
            if(null != juheCity){
                return juheCity.getCityName();
            }
        }
        return null;
    }
	
	@Override
	public void saveUpdateAndDelete(JuheCity juheCity, OperateType operType) {
		switch (operType) {
		case add:
			juheCity.initInsertData(juheCity.getModifier(), juheCity.getModifierName());
			juheCityMapper.insert(juheCity);
			break;
		case edit:
			juheCity.initUpdateData(juheCity.getModifier(), juheCity.getModifierName());
			// 影响的记录数
			int updateCount = juheCityMapper.updateByPrimaryKeySelective(juheCity);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			juheCity.initDeleteData(juheCity.getModifier(), juheCity.getModifierName());
			// 影响的记录数
			int deleteCount = juheCityMapper.updateByPrimaryKeySelective(juheCity);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheCity> selectByExt(JuheCityExt juheCityExt) {
		return juheCityMapper.selectByExt(juheCityExt);
	}
	
	@Override
	public int countByExt(JuheCityExt juheCityExt){
		return juheCityMapper.countByExt(juheCityExt);
	}
	
	@Override
	public JuheCity selectById(String id) {
		return juheCityMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheCityMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(JuheCity juheCity) {
		return juheCityMapper.countRefined(juheCity);
	}

	@Override
	public JuheCity selectRefined(JuheCity juheCity) {
		return juheCityMapper.selectRefined(juheCity);
	}
	
	@Override
	public int deletedRefined(JuheCity juheCity) {
		return juheCityMapper.deletedRefined(juheCity);
	}

	@Override
	public boolean repeat(JuheCity juheCity) {
		int count = countRefined(juheCity);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(juheCity.getId())) {
			return true;
		}
		JuheCity dbItem = selectRefined(juheCity);
		if(null == dbItem) {
			return false;
		}
		return !juheCity.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return juheCityMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<JuheCity> juheCitys) {
		return juheCityMapper.insertBatch(juheCitys);
	}
}
